﻿using MahApps.Metro.Controls;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Research.DynamicDataDisplay;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Test.Model;
using Test.Utils;
using System.Data.SQLite;

namespace Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {

        private ObservableDataSource<Point> dataSource1 = new ObservableDataSource<Point>();
        private ObservableDataSource<Point> dataSource2 = new ObservableDataSource<Point>();
        private ObservableDataSource<Point> dataSource3 = new ObservableDataSource<Point>();

        private PerformanceCounter cpuPerformance = new PerformanceCounter();
        private DispatcherTimer timer = new DispatcherTimer();
        private int i = 0;


        public int fistResult = 0;
        public int secondResult = 0;
        public int equalResult = 0;

        Dictionary<string,string> radioDic= new Dictionary<string,string>();
        private string checkedRadio = "rbtnEqual";
    
        public MainWindow()
        {
            InitializeComponent();
            this.gridRGB.AddHandler(TextBox.GotFocusEvent, new RoutedEventHandler(this.TextBox_GotFocusEvent));

            //radioDic
            radioDic.Add("rbtnFirst", "fistResult");
            radioDic.Add("rbtnSecond", "secondResult");
            radioDic.Add("rbtnEqual", "equalResult");


            // list
            List<string> list=new List<string>();
            for (int i = 0; i < 50; i++)
			{
			 list.Add("band   "+i);
			}
            lbxBand.ItemsSource = list;

            //image
            string path = DataStrings.image_path;
            imgRGB.Source = new BitmapImage(new Uri(path,UriKind.Relative));

        }

        private void AnimatedPlot(object sender, EventArgs e)
        {
            cpuPerformance.CategoryName = "Processor";
            cpuPerformance.CounterName = "% Processor Time";
            cpuPerformance.InstanceName = "_Total";

            double x = i;
            double y = cpuPerformance.NextValue();

            Point point = new Point(x, y);
            //dataSource.AppendAsync(base.Dispatcher, point);

            i++;
        }

        private void TextBox_GotFocusEvent(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is ListBoxItem)
            {
                if (!DataStrings.click_textbox1.Equals("") || !DataStrings.click_textbox2.Equals(""))
                {
                     TextBox tb;
                     if (!DataStrings.click_textbox1.Equals(""))
                     {

                         tb = spRGB.FindName(DataStrings.click_textbox1) as TextBox;
                     }
                     else {
                         tb = spRGB.FindName(DataStrings.click_textbox2) as TextBox;
                     }
                    tb.Text = (string)(e.OriginalSource as ListBoxItem).Content;
                    DataStrings.click_textbox1 = "";
                    //Console.WriteLine("ss");
                }
                else {
                    DataStrings.click_textbox1 = "";
                }
            }
            else if (e.OriginalSource is TextBox)
            {
                DataStrings.click_textbox1 = (e.OriginalSource as FrameworkElement).Name;
                DataStrings.click_textbox2 = DataStrings.click_textbox1;
            }
            else {
                DataStrings.click_textbox1 = "";
                DataStrings.click_textbox2 = "";
            }
            //Console.WriteLine("vvv");
            e.Handled = true;
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            populatePlotter();
            plotter.AddLineGraph(dataSource1, Colors.Green, 2, "SAM");
            plotter.AddLineGraph(dataSource2, Colors.Red, 2, "SAM_Distance");
            plotter.AddLineGraph(dataSource3, Colors.Blue, 2, "Cuprite");
            plotter.Viewport.FitToView();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string va="";
            radioDic.TryGetValue(checkedRadio, out va);
            //赋值
            int oldNum=(int) this.GetType().GetField(va).GetValue(this);
            this.GetType().GetField(va).SetValue(this,++oldNum);
            tbkEvaluation.Text = "first\t:  " + fistResult + "\n" + "second\t:  " + secondResult + "\n" + "equal\t:  " + equalResult + "\n";


            populatePlotter();
            
        }

        private void rbtn_Checked(object sender, RoutedEventArgs e)
        {
            checkedRadio = (sender as RadioButton).Name;
        }

        //加载曲线

        private void populatePlotter() {
            string path = "d:/git/xiong-test/" + "Data/all_s.txt";
            string[][] datas= HyperFileUtil.ReadDataToArray(path);

            string path1 = "d:/git/xiong-test/" + "Data/indsam.txt";
            string[][] datas1 = HyperFileUtil.ReadDataToArray(path1);

            string path2 = "d:/git/xiong-test/" + "Data/indsamDistance.txt";
            string[][] datas2 = HyperFileUtil.ReadDataToArray(path2);

            string path3 = "d:/git/xiong-test/" + "Data/cuprite_47750_187.txt";
            string[][] datas3 = HyperFileUtil.ReadDataToArray(path3);

            //生成随机数
            Random rd = new Random();
            int ind=rd.Next(0,47749);

            int ind1=0;
            int.TryParse(datas1[0][ind], out ind1);
            string[] source1 = datas[ind1];

            int.TryParse(datas2[0][ind], out ind1);
            string[] source2 = datas[ind1];

            string[] source3 = datas3[ind];

            List<Point> list1 = new List<Point>();
            List<Point> list2 = new List<Point>();
            List<Point> list3 = new List<Point>();
            double temp=0;
            for (int i = 0; i < source1.Length; i++)
            {

                double.TryParse(source1[i],out temp);
                list1.Add(new Point(i + 1, temp));

                double.TryParse(source2[i], out temp);
                list2.Add(new Point(i + 1, temp));

                double.TryParse(source3[i], out temp);
                list3.Add(new Point(i + 1, temp));
            }
            dataSource1.Collection.Clear();
            dataSource2.Collection.Clear();
            dataSource3.Collection.Clear();

            dataSource1.AppendMany(list1);
            dataSource2.AppendMany(list2);
            dataSource3.AppendMany(list3);
            
        }


        private string dataBaseSource = "D:/sqlite_databases/TestDatabase.db";
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SQLiteConnection dbConn = new SQLiteConnection("Data Source=" + dataBaseSource);
            dbConn.Open();
            string voter = "xiong";
            DateTime time = DateTime.Now;
            string voteResults = "sam:100_distance:100";
            addOneRow(dbConn, voter, time, voteResults);
        }

        private void addOneRow(SQLiteConnection conn, string voter, DateTime time, string voteResults)
        {
            SQLiteCommand insertSql = new SQLiteCommand("INSERT INTO VotesTable (VoteId, voter, time, vote_result)" +
                                " VALUES (@VoteId, @voter, @time, @vote_result)", conn);
            insertSql.Parameters.AddWithValue("@VoteId", null);
            insertSql.Parameters.AddWithValue("@voter", voter);
            insertSql.Parameters.AddWithValue("@time", time);
            insertSql.Parameters.AddWithValue("@vote_result", voteResults);
            insertSql.ExecuteNonQuery();
            Console.WriteLine(insertSql.ToString());
        }
      
    }
}
